package com.example.news.service;

import com.example.news.bean.News;
import com.example.news.bean.User;
import com.example.news.mapper.NewsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class NewsService {
    @Autowired
    NewsMapper newsMapper;
    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    /**
     * 新增新闻
     * @param news
     * @return
     */
    public Map<String, Object> insertNews(News news) {
        Map<String,Object>  resp = new HashMap<String,Object>();
        news.setDate(sf.format(new Date()));
        int i = newsMapper.insertNews(news);
        if (i > 0){
            resp.put("code",200);
            resp.put("msg","发布成功");
        }else {
            resp.put("code",201);
            resp.put("msg","发布失败");
        }
        return resp;
    }

    /**
     * 通过类型id查询新闻
     * @param type_id
     * @return
     */
    public Map<String, Object> selectNewsByTypeId(Integer type_id) {
        Map<String,Object>  resp = new HashMap<String,Object>();
        List<News> list = newsMapper.selectNewsByTypeId(type_id);
        resp.put("code",200);
        resp.put("msg","查询成功");
        resp.put("data",list);
        return resp;
    }

}
